import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/commom/Dio.dart';
import 'package:flutter_app/commom/Global.dart';
import 'package:flutter_app/widgets/AplResultCard.dart';
import 'package:flutter_app/widgets/HYSizeFit.dart';
import 'package:flutter_app/widgets/ModifyUserResultCard.dart';

import 'package:flutter_app/widgets/RecomProjectItem.dart';
import 'package:flutter_app/widgets/ReleasePjResultCard.dart';
import 'package:flutter_slidable/flutter_slidable.dart';

import 'FriendListRoute.dart';

class HYHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 初始化HYSizeFit
    HYSizeFit.initialize(context);
    return null;
  }
}

class HomePageRoute extends StatefulWidget {
  HomePageRoute({Key key}) : super(key: key);
  @override
  HomePageRouteState createState() => HomePageRouteState();
}

class HomePageRouteState extends State<HomePageRoute> {

  @override
  void initState() {
    // TODO: implement initState
    print(Global.findFriendApl);
    print(Global.findUndertakeApl);
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    HYSizeFit.initialize(context);
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text(''),
        actions: <Widget>[
          //导航栏右侧菜单
          IconButton(
            icon: Icon(Icons.account_circle),
            onPressed: () => {
              Navigator.push(
                context,
                new MaterialPageRoute(builder: (context) {
                  return new Friend_WaitingPage();
                }),
              )
            },
          )
        ],
        leading: Builder(builder: (context) {
          return IconButton(
              icon:
              Icon(Icons.chat_bubble_outline, color: Colors.black), //自定义图标
              onPressed: () {

              });
        }
        ),
      ),
      backgroundColor:Colors.white,
      body: IntrinsicHeight(
        child: Flex(
          direction: Axis.vertical,
          children: <Widget>[
            Column(
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.only(top: 5.0),
                  child: Text("欢迎使用",
                      //textScaleFactor: 3,
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        color: Colors.grey[700],
                        fontSize: HYSizeFit.setPx(30),
                        height: 1.2,
                        fontWeight: FontWeight.w900,
                      )),
                ),
                Padding(
                  //top: 40,
                  padding: const EdgeInsets.only(top: 5.0),
                  child: Text("企业帮!",
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        color: Colors.grey[700],
                        fontSize: HYSizeFit.setPx(30),
                        height: 1.2,
                        fontWeight: FontWeight.w900,
                      )),
                ),
                Padding(
                    padding: const EdgeInsets.only(top: 5.0),
                    child: DecoratedBox(
                        decoration: BoxDecoration(color: Colors.grey[700]),
                        child: Padding(
                          padding: EdgeInsets.symmetric(
                              horizontal: HYSizeFit.setPx(50), vertical: 1.5),
                        ))),
              ],
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.only(top: 5.0, left: 10),
                  child: Container(
                    padding: EdgeInsets.all(5),
                    margin: EdgeInsets.only(left: 5),
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.all(Radius.circular(5)),
                        color:Colors.black54
                    ),
                    child: Text(
                      "最近的消息",
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: HYSizeFit.setPx(14),
                      ),
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 10.0),
                  child: Container(
                    width: HYSizeFit.setPx(400),
                    height: HYSizeFit.setRpx(400),
                    child: ListView.separated(
                      padding: EdgeInsets.fromLTRB(HYSizeFit.setPx(20), 0, HYSizeFit.setPx(20), 0),
                      itemCount: (Global.recentDisposeItem == null)? 0 : Global.recentDisposeItem.length,
                      //itemCount: titles.length,
                      shrinkWrap: true,
                      //列表项构造器
                      itemBuilder: (context, index){
                        var item = Global.recentDisposeItem[index];
                        return RecentAplItem(index, item);
                      },
                      separatorBuilder: (BuildContext context, int index) {
                        return Container(
                          color: Colors.grey[900],
                        );
                      },
                    ),
                  ),
                ),
              ],
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Padding(
                    padding: const EdgeInsets.only(top: 5.0, left: 10, right: 20),
                    child: Row(
                      //mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        Container(
                          padding: EdgeInsets.all(5),
                          margin: EdgeInsets.only(left: 5),
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.all(Radius.circular(5)),
                              color:Colors.black54
                          ),
                          child: Text(
                            "为您推荐",
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: HYSizeFit.setPx(14),
                            ),
                          ),
                        ),
                        Expanded(child: SizedBox()),
                        InkWell(
                          onTap: (){

                          },
                          child: Row(

                            children: [
                              Icon(
                                Icons.autorenew,
                                size: 17,
                                color: Colors.black54,
                              ),
                              Container(width: 12),
                              //文字
                              Text(
                                '换一批',
                                style: TextStyle(
                                    color: Colors.black54,
                                    fontSize: 15,
                                    fontWeight: FontWeight.bold
                                ),
                              ),
                            ],
                          ),
                        )
                      ],
                    )
                ),
                Padding(
                  //top: 330,
                  padding: const EdgeInsets.only(top: 10.0),
                  child: Container(
                    width: HYSizeFit.setPx(400),
                    height: HYSizeFit.setRpx(420),
                    child: ListView.builder(
                        padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                        itemCount: 0,
                        itemExtent: HYSizeFit.setPx(90),
                        shrinkWrap: true,
                        //列表项构造器
                        itemBuilder: RecomProjectItem),
                  ),
                ),
              ],
            ),
          ],
        ),
      )

    );
  }

  Widget RecentAplItem(int position, var item){
    return Slidable.builder(
        actionPane: SlidableScrollActionPane(),
        actionExtentRatio: 0.25,
        child: Container(
          height: HYSizeFit.setPx(40),
          width: double.infinity,
          color: Colors.white10,
          alignment: Alignment.centerLeft,
          child: Text(
            //"消息${datas[position]}",
            //Global.findFriendApl.applyFriends.isNotEmpty ?
            //"${Global.findFriendApl.applyFriends[index]['aplUser']['usrName'].toString()}请求添加您为好友":'',
            item['message'],
            textAlign: TextAlign.left,
            style: TextStyle(
              color: Colors.black87,
              fontSize: HYSizeFit.setPx(16),
            ),
          ),
        ),

        secondaryActionDelegate: SlideActionBuilderDelegate(
            actionCount: item['sort'] == 1 || item['sort'] == 2 ? 2 : 1,
            builder: (context, index, animation, renderingMode){
                if(item['sort'] == 1 || item['sort'] == 2){
                  if(index == 0){
                    return GestureDetector(
                        child: Text(
                          "同意",
                          style: TextStyle(
                            color: Colors.black87,
                            fontSize: HYSizeFit.setPx(15),
                          ),
                        ),
                        onTap: ()async{
                          if(Global.UserInfo){
                            var status = await _onDisposeApl(item['Id'], true, context, item['sort']);
                            if(status == 200)
                            {
                              Scaffold.of(context).showSnackBar(SnackBar(content: Text('处理成功')));
                              setState(() {
                                if(Global.recentDisposeItem.isNotEmpty){
                                  Global.recentDisposeItem.removeAt(position);
                                }
                              });
                            }
                            else Scaffold.of(context).showSnackBar(SnackBar(content: Text('处理失败')));
                          }
                          else await NotifiedCard(context);
                        }
                    );
                  }
                  else{
                    return GestureDetector(
                        child: Text(
                          "拒绝",
                          style: TextStyle(
                            color: Colors.black87,
                            fontSize: HYSizeFit.setPx(15),
                          ),
                        ),
                        onTap: ()async{
                          if(Global.UserInfo){
                            var status = await _onDisposeApl(item['Id'], false, context, item['sort']);
                            if(status == 200)
                            {
                              Scaffold.of(context).showSnackBar(SnackBar(content: Text('处理成功')));
                              setState(() {
                                if(Global.recentDisposeItem.isNotEmpty){
                                  Global.recentDisposeItem.removeAt(position);
                                }
                              });
                            }
                            else Scaffold.of(context).showSnackBar(SnackBar(content: Text('处理失败')));
                          }
                          else await NotifiedCard(context);
                        }
                    );
                  }
                }else return GestureDetector(
                    child: Text(
                      "已读",
                      style: TextStyle(
                        color: Colors.black87,
                        fontSize: HYSizeFit.setPx(15),
                      ),
                    ),
                    onTap: ()async{
                      var status = await _onDisposeApl(item['aplId'], true, context, item['sort']);
                      if(status == 200){
                        Scaffold.of(context).showSnackBar(SnackBar(content: Text('处理成功')));
                        setState(() {
                          if(Global.recentDisposeItem.isNotEmpty){
                            Global.recentDisposeItem.removeAt(position);
                          }
                        });
                      }
                      else Scaffold.of(context).showSnackBar(SnackBar(content: Text('已清除')));
                    }
                );
            }
        )
    );
  }
  Future<int> _onDisposeApl(int Id, bool flag, BuildContext context, int sort) async{
    var status = 201;
    bool result = false;
    switch(sort){
      case 1: { status = await EA(context). disposeFriendApl(Id, flag); }
      break;
      case 2: { result = await EA(context).disposeUndertakeApl(Id, flag); if(result) status = 200;}
      break;
      case 3: { status = await EA(context).isReadFriend(Id); }
      break;
      case 4: { status = await EA(context).isReadProcedure(Id);}
      break;
      default: { print("Invalid choice"); }
      break;
    }
    return status;
  }

}